﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using VietThinks.Contract;
using VietThinks.Model;
using System.Linq.Expressions;

namespace VietThinks.Service
{
    internal static partial class Convert
    {
        static Convert()
        {
            CreateMaps();
        }

        static void CreateMaps()
        {
            //Mapper.CreateMap<Product, ProductDto>().Bidirectional();
               // .ForMemberEx(dto=>dto.Name,product=>product.Name)
            // .ForMemberEx(dto => dto.Price, product => product.Price);
            #region "Admin"
            Mapper.CreateMap<Role, RoleDto>().Bidirectional();
            Mapper.CreateMap<Right, RightDto>().Bidirectional();
            Mapper.CreateMap<RightInRole, RightInRoleDto>().Bidirectional();
            Mapper.CreateMap<DeniedRight, DeniedRightDto>().Bidirectional();
            #endregion
            #region "Account"
            Mapper.CreateMap<Account, AccountDto>().Bidirectional();
            Mapper.CreateMap<AccountInfo, AccountInfoDto>().Bidirectional();
            Mapper.CreateMap<Position, PositionDto>().Bidirectional();
            Mapper.CreateMap<Staff, StaffDto>().Bidirectional();
            #endregion
            #region "Product"
            Mapper.CreateMap<Image, ImageDto>().Bidirectional();
            Mapper.CreateMap<Option, OptionDto>().Bidirectional();
            Mapper.CreateMap<Product, ProductDto>().Bidirectional();
            Mapper.CreateMap<ProductImage, ProductImageDto>().Bidirectional();
            Mapper.CreateMap<ProductOption, ProductOptionDto>().Bidirectional();
            Mapper.CreateMap<Promotion, PromotionDto>().Bidirectional();
            #endregion
            #region "VThinks"
            Mapper.CreateMap<Classification, ClassificationDto>().Bidirectional();
            Mapper.CreateMap<Manufacturer, ManufacturerDto>().Bidirectional();
            Mapper.CreateMap<Province, ProvinceDto>().Bidirectional();
            Mapper.CreateMap<Supplier, SupplierDto>().Bidirectional();
            Mapper.CreateMap<Unit, UnitDto>().Bidirectional();
            #endregion
        }
    }
    internal static class IMappingExpressionExtensions
    {
        public static IMappingExpression<TSource, TDestination> ForMemberEx<TSource, TDestination>(
            this IMappingExpression<TSource, TDestination> mappingExpression,
            Expression<Func<TDestination, object>> destinationMember,
            Func<TSource, object> sourceMember)
        {
            return mappingExpression.ForMember(destinationMember, cfg => cfg.MapFrom(sourceMember));
        }

        public static IMappingExpression<TSource, TDestination> Ignore<TSource, TDestination>(
            this IMappingExpression<TSource, TDestination> mappingExpression,
            Expression<Func<TDestination, object>> destinationMember)
        {
            return mappingExpression.ForMember(destinationMember, cfg => cfg.Ignore());
        }

        public static IMappingExpression<TSource, TDestination> Bidirectional<TSource, TDestination>(
            this IMappingExpression<TSource, TDestination> mappingExpression)
        {
            Mapper.CreateMap<TDestination, TSource>();

            return mappingExpression;
        }
    }
}
